package com.virtium.vpm.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.virtium.vpm.controller.UserSession;
import com.virtium.vpm.controller.UserSessionManager;
import com.virtium.vpm.util.FormUtil;
import com.virtium.vpm.util.StringUtil;



/** 
 * VPM 
 * Creation date: 01-12-2010
 *
 */
public class DoLogin extends Action {
	

	/** 
	 * Method execute
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception{
		String userName = request.getParameter("u");
		String password = request.getParameter("p");
		String flag = "failue";
		try{
			HttpSession session = request.getSession();
	        FormUtil.clearAllSessionAttributes(session);
	        UserSession user;
	        UserSessionManager sessionMgr = new UserSessionManager();
	        if (!StringUtil.isEmpty(userName) && !StringUtil.isEmpty(password)) {
	        	user = new UserSession();
	        	user = sessionMgr.authenticateUser(userName, password, session, request);
	        	if(user != null){
	        		session.setAttribute("SESSION_USER",user);
	        		flag = "success";
	        	}
	        }
		}catch(Exception ex){}
		response.setContentType("application/text");
        response.getWriter().write(flag);
        
		return null;
	}
}